package entity;

import com.hzheima.util.ImageUtil;
import constant.UiConstant;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * @Author:zhengbowen
 * @Date: 2025/09/09
 * @Description:飞机大招类
 */
public class Bigkill {
    private ArrayList<Bullet> superBullets = new ArrayList<>();
    // 大招激活状态
    private boolean isActive = false;
    // 大招持续时间计数器
    private int duration = 0;
    // 大招最大持续时间
    private static final int MAX_DURATION = 60;
    private BufferedImage superSkillImage;
    // 图片位置和尺寸
    private int imageX, imageY, imageWidth, imageHeight;
    // 清屏标记
    private boolean shouldClearScreen = false;

    public Bigkill() {
        try {
            superSkillImage = ImageUtil.getImg("大招.png");
        } catch (Exception e) {
            System.out.println("无法加载大招特效图片: " + e.getMessage());
        }
        imageWidth = UiConstant.APP_WIDTH;
        imageHeight = UiConstant.APP_HEIGHT;
        imageX = 0;
        imageY = 0;
    }
    public void activate(Plane plane) {
        if (!isActive) {
            isActive = true;
            duration = 0;
            shouldClearScreen = true;

            // 创建全屏范围的大招子弹网格
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 10; j++) {
                    Bullet bullet = new Bullet(i * 50, j * 50,1);
                    bullet.setySpeed(-15); // 设置子弹向上移动的速度
                    superBullets.add(bullet);
                }
            }
        }
    }

    //更新大招状态
    public void update() {
        if (isActive) {
            duration++;
            // 移动所有大招子弹并移除超出屏幕的子弹
            superBullets.removeIf(bullet -> {
                bullet.move();
                return bullet.getY() < -50;
            });
            // 检查大招是否结束（达到最大持续时间或没有子弹）
            if (duration >= MAX_DURATION || superBullets.isEmpty()) {
                isActive = false;
                superBullets.clear();
            }
        }
    }

    public void draw(Graphics g) {
        if (isActive) {
            // 绘制大招子弹
            for (Bullet bullet : superBullets) {
                g.setColor(Color.CYAN);
                g.fillOval(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
            }

            // 绘制大招特效图片
            if (superSkillImage != null) {
                // 添加闪烁效果
                float alpha = 0.7f + 0.3f * (float) Math.sin(duration * 0.3);
                if (g instanceof Graphics2D) {
                    Graphics2D g2d = (Graphics2D) g;
                    Composite originalComposite = g2d.getComposite();
                    AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
                    g2d.setComposite(alphaComposite);
                    g.drawImage(superSkillImage, imageX, imageY, imageWidth, imageHeight, null);
                    g2d.setComposite(originalComposite);
                } else {
                    g.drawImage(superSkillImage, imageX, imageY, imageWidth, imageHeight, null);
                }
            } else {
                // 如果没有图片，则绘制默认特效文字
                g.setColor(new Color(0, 255, 255, 100));
                g.setFont(new Font("楷体", Font.BOLD, 30));
                g.drawString("大招释放中...", UiConstant.APP_WIDTH / 2 - 80, 100);
            }
        }
    }

    public boolean isActive() {
        return isActive;
    }

    public ArrayList<Bullet> getBullets() {
        return superBullets;
    }

    public BufferedImage getSuperSkillImage() {
        return superSkillImage;
    }

    public boolean shouldClearScreen() {
        return shouldClearScreen;
    }

    public void clearScreenFlag() {
        shouldClearScreen = false;
    }
}
